package com.example.yiqiuser.service.impl;


import com.example.yiqiuser.dao.PicDao;
import com.example.yiqiuser.entity.Pic;
import com.example.yiqiuser.service.PicService;
import com.example.yiqiuser.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ fileName:PicServiceImpl
 * @ description:
 * @ author:诸葛龙辰
 * @ createTime:2023/3/29 14:59
 * @ version:1.0.0
 */
@Service
public class PicServiceImpl implements PicService {
    //依赖注入
    @Resource
    private PicDao picDao;

    @Override
    public PageInfo queryPage(Page<Pic> page) {
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(picDao.queryByParam(page.getData()));
    }

    @Override
    public List<Pic> queryAll() {
        return picDao.queryAll();
    }

    @Override
    public Pic queryById(Integer id) {
        List<Pic> picList = picDao.queryById(id);
        if(picList != null && picList.size()>0){
            return picList.get(0);
        }
        return null;
    }


    @Override
    public int add(Pic pic) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String dateStr = sdf.format(date);
        pic.setSctime(dateStr);
        return picDao.add(pic);
    }

    @Override
    public int update(Pic pic) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String dateStr = sdf.format(date);
        pic.setSctime(dateStr);
        return picDao.update(pic);
    }

    @Override
    public int deleteById(Pic pic) {
        Integer id = pic.getId();
        return picDao.deleteById(id);
    }
}
